// ===== 函数声明 =====
// 方式 1：经典版
function greet1(name: string): string {
    return `Hi! ${name}`;
}
// 方式 2：函数作为常量
const greet2 = function (name: string): string {
    return `Hi! ${name}`;
}
// 方式 3：箭头函数
const greet3 = (name: string): string => {
    return `Hi! ${name}`;
}
// 方式 4：函数类型
type GreetFuncType = (name: string) => string;
const greetNamed3: GreetFuncType = name => `Hi! ${name}`;

// 函数的参数
// 特例 1：可选参数
function buildName(firstName: string, lastName?: string) {
    if (lastName)
        return firstName + " " + lastName;
    else
        return firstName;
}
let name1 = buildName("Bob");  // lastName is undefined now
let name2 = buildName("Bob", "Adams");

// 特例 2：默认参数
function buildNameWithDefault(firstName: string, lastName = "Smith") {
    if (lastName)
        return firstName + " " + lastName;
    else
        return firstName;
}
let name3 = buildNameWithDefault("Bob");  // lastName is "Smith" now
let name4 = buildNameWithDefault("Bob", "Adams");

// 特例 3：其余参数
function buildNameRest(firstName: string, ...restOfName: string[]): string {
    return firstName + " " + restOfName.join(" ");
}
let employeeName = buildNameRest("Joseph", "Samuel", "Lucas", "MacKinzie");

// ===== 函数重载 =====
function test(name: string): string;
function test(age: number): number;
function test(value: string | number): any {
    switch (typeof value) {
        case 'string':
            return `my name is ${value}`;
        case 'number':
            return value + 1;
    }
}

function demo(s: string, n: number): void;
function demo(s: string): void;
function demo(s: string, n: number = 10) {
    console.log(`${s}:${n}`)
}

demo('Dirac')